<?php
/**
 * 附件管理 - JBlog
 *
 * @copyright (c) 2008-2012 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: Attach.php 30 2012-10-15 06:08:28Z lisijie86@gmail.com $
 */

class AttachController extends BaseController {
	
	protected function _init_() {
		$this->table = $this->db->getTable('attach');
		$this->addTab(__('附件管理'), 'attach.index');
	}
	
	/**
	 * 附件列表
	 * 
	 * @param array $params
	 */
	public function indexAction($params) {
		$year     = intval($params['year']);     //搜索年份
		$month    = intval($params['month']);    //搜索月份
		$day      = intval($params['day']);      //搜索日期
		$keyword  = String::safeStr($params['keyword']); //搜索关键字
		$fileext  = String::safeStr($params['fileext']); //搜索扩展名
		$sort     = $params['sort'];             //排序字段
		$sorttype = $params['sorttype'];         //排序方式
		$page     = max(1, intval($params['page']));
		$pagesize = 20;
		$wheresql = $orderby = $querystring = '';
		if (!in_array($sort, array('id','filesize','downloads'))) {
			$sort = '';
		}
		$sorttype != 'asc' && $sorttype = 'desc';
		
		if ($year > 0) {
			$stime = mktime(0,0,0,max(1,$month),max(1,$day),$year);
			if ( $month ) {
				$etime = strtotime('+1 month', $stime);
				if ( $day ) {
					$etime = strtotime('+1 day', $stime);
				}
			} else {
				$etime = strtotime('+1 year', $stime);
			}
			$wheresql .= " AND dateline BETWEEN $stime AND $etime";
			$querystring .= "&year={$year}&month={$month}&day={$day}";
		}
		if ( !empty($fileext) ) {
			$wheresql .= " AND fileext = '$fileext'";
			$querystring .= "&fileext={$fileext}";
		}
		if ( $keyword ) {
			$wheresql .= " AND originalname LIKE '%{$keyword}%'";
			$querystring .= "&keyword=".rawurlencode($keyword);
		}
		if (!empty($sort)) {
			$orderby = "{$sort} {$sorttype}";
			$querystring .= "&sort={$sort}&sorttype={$sorttype}";
		} else {
			$orderby = "id DESC";
		}
		
		$count = $this->db->count($this->table, "1 $wheresql");
		$list = array();
		if ( $count ) {
			$sql = "SELECT * FROM {$this->table} WHERE 1 $wheresql ORDER BY $orderby";
			$list = $this->db->getList($sql, $page, $pagesize);
			foreach ($list as $key => $value) {
				$value['filesize'] = File::sizeFormat($value['filesize']);
				$value['dateline'] = self::date($value['dateline']);
				$list[$key] = $value;
			}
		}
		
		$pager = new Pager($page, $count, $querystring);
		$this->assign('keyword', $keyword);
		$this->assign('list', $list);
		$this->assign('fileExtOptions', $this->getFileExtOptions($this->input->fileext));
		$this->assign('dateOptions', $this->getDateOptions($year, $month, $day));
		$this->assign('pageBar', $pager->makeHtml());
		$this->display();
	}
	
	/**
	 * 删除附件
	 */
	public function deleteAction() {
		$ids = (array)$this->input->get('id');
		foreach ($ids as $k => $v) {
			if (!is_numeric($v)) {
				unset($ids[$k]);
			}
		}
		$result = $this->db->getList("SELECT filepath FROM {$this->table} WHERE id IN (".simplode($ids).")");
		foreach ($result as $row) {
			@unlink(DATA_PATH.$row['filepath']);
		}
		$this->db->execute("DELETE FROM {$this->table} WHERE id IN (".simplode($ids).")");
		$this->redirect($this->refer);
	}
	
	/**
	 * 生成文件扩展名下拉菜单
	 */
	private function getFileExtOptions($selext = '') {
		$ret = '';
		$result = $this->db->getList("SELECT DISTINCT fileext FROM {$this->table}");
		foreach ($result as $row) {
			$selstr = $selext == $row['fileext'] ? 'selected="selected"' : '';
			$ret .= '<option vlaue="'.$row['fileext'].'" '.$selstr.'>'.$row['fileext'].'</option>';
		}
		return $ret;
	}
	
	/**
	 * 生成日期选择下拉菜单
	 * 
	 * @param int $year
	 * @param int $month
	 * @param int $day
	 */
	private function getDateOptions($year=0, $month=0, $day=0) {
		$ret = '';
		$result = $this->db->getRow("SELECT MIN(dateline) AS mindate,MAX(dateline) AS maxdate FROM {$this->table}");
		$minyear = self::date($result['mindate'],'Y');
		$minmonth = self::date($result['mindate'],'n');
		$maxyear = self::date($result['maxdate'],'Y');
		$maxmonth = self::date($result['maxdate'],'n');
		$ret .= '<select name="year"><option value=""></option>';
		for($i=$minyear; $i<=$maxyear; $i++) {
			$selstr = $year == $i ? 'selected="selected"' : '';
			$ret .= '<option value="'.$i.'" '.$selstr.'>'.$i.'</option>';
		}
		$ret .= '</select>'.__('年');
		if ($minyear != $maxyear) {
			$minmonth = 1; $maxmonth = 12;
		}
		$ret .= '<select name="month"><option value=""></option>';
		for($i=$minmonth; $i<=$maxmonth; $i++) {
			$selstr = $month == $i ? 'selected="selected"' : '';
			$ret .= '<option value="'.$i.'" '.$selstr.'>'.$i.'</option>';
		}
		$ret .= '</select>'.__('月');
		$ret .= '<select name="day"><option value=""></option>';
		for($i=1; $i<=31; $i++) {
			$selstr = $day == $i ? 'selected="selected"' : '';
			$ret .= '<option value="'.$i.'" '.$selstr.'>'.$i.'</option>';
		}
		$ret .= '</select>'.__('日');
		return $ret;
	}
}